﻿using System;
using Gtk;
using Levi.Finance.Client;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using Newtonsoft.Json;
using Newtonsoft.Json.Utilities;

public partial class ListWindow : Gtk.Window
{
	public ABC g;
	public ListWindow(ABC _g)
		: base(Gtk.WindowType.Toplevel)
	{
		Build();
		this.g = _g;
		//bntSubmit.Clicked += new EventHandler(btnSubmitClick);
	}

	protected void OnDeleteEvent(object sender, DeleteEventArgs a)
	{
		g.win.Destroy();
		Application.Quit();
		a.RetVal = true;
	}

	protected void BtnSubmitClick(object obj, EventArgs args)
	{
		//string username = this.username.Text.ToString();
		//string password = this.password.Text.ToString();
		Package pack = new Package();
		pack.opt = 1;
		//pack.username = username;
		//pack.password = password;
		//string str = JsonConvert.SerializeObject(pack);
		//str = System.Text.Encoding.Default.g
		g.query.SendMessage(JsonConvert.SerializeObject(pack));
	}

	public void FinanceTree(ArrayList array)
	{
		// Create our TreeView
		Gtk.TreeView tree = new Gtk.TreeView();

		// Create a box to hold the Entry and Tree
		Gtk.VBox box = new Gtk.VBox();

		// Add the widgets to the box
		box.PackStart(tree, true, true, 5);

		box.SetSizeRequest(790,390);

		g.list.Add(box);

		// Create a column for the artist name
		Gtk.TreeViewColumn remark = new Gtk.TreeViewColumn();
		remark.Title = "消费原因";

		// Create the text cell that will display the artist name
		Gtk.CellRendererText remarkCell = new Gtk.CellRendererText();

		// Add the cell to the column
		remark.PackStart(remarkCell, true);

		// Create a column for the song title
		Gtk.TreeViewColumn incomeColumn = new Gtk.TreeViewColumn();
		incomeColumn.Title = "收入/支出";

		// Do the same for the song title column
		Gtk.CellRendererText incomeCell = new Gtk.CellRendererText();
		incomeColumn.PackStart(incomeCell, true);

		// Create a column for the song title
		Gtk.TreeViewColumn moneyColumn = new Gtk.TreeViewColumn();
		moneyColumn.Title = "金额";

		// Do the same for the song title column
		Gtk.CellRendererText moneyCell = new Gtk.CellRendererText();
		moneyColumn.PackStart(moneyCell, true);

		// Create a column for the song title
		Gtk.TreeViewColumn dateColumn = new Gtk.TreeViewColumn();
		dateColumn.Title = "时间";

		// Do the same for the song title column
		Gtk.CellRendererText dateCell = new Gtk.CellRendererText();
		dateColumn.PackStart(dateCell, true);

		// Add the columns to the TreeView
		tree.AppendColumn(remark);
		tree.AppendColumn(incomeColumn);
		tree.AppendColumn(moneyColumn);
		tree.AppendColumn(dateColumn);

		// Tell the Cell Renderers which items in the model to display
		remark.AddAttribute(remarkCell, "text", 0);
		incomeColumn.AddAttribute(incomeCell, "text", 1);
		moneyColumn.AddAttribute(moneyCell, "text", 2);
		dateColumn.AddAttribute(dateCell, "text", 3);

		// Create a model that will hold two strings - Artist Name and Song Title
		Gtk.ListStore FinanceListStore = new Gtk.ListStore(typeof(string), typeof(string), typeof(string), typeof(string));

		// Add some data to the store
		foreach(object row in array){
			Finance f = new Finance();
			f = (Finance)row;
			string sRemark = f.remark;
			string income = null;
			if(f.income == 0)
				income = "支出";
			else
				income = "收入";
			string money = f.money;
			string date = Silmoon.SpecialConverter.ConvertIntDateTime(f.createTime).ToString();
			FinanceListStore.AppendValues(sRemark,income,money,date);
		}
		tree.Model = FinanceListStore;
		g.list.ShowAll();
	}

	public void AlertMsg(string text)
	{
		//this.label3.Text = text;
	}
}
